package pf

import (
	"go.manyji.com/lib/net/socks5"
	"go.manyji.com/lib/net/socks5/socks5http"
	"net/http"
	"net/url"
	"strings"
)

type Client struct {
	proxy *socks5.Proxy
}

func Http(proxy *socks5.Proxy) *Client {
	return &Client{proxy: proxy}
}

func (c *Client) cli() *http.Client {
	if c.proxy != nil {
		return socks5http.NewClient(c.proxy)
	}
	return http.DefaultClient
}

func (c *Client) Get(url string, makeReq func(r *http.Request)) (*http.Response, error) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	if makeReq != nil {
		makeReq(req)
	}
	return c.cli().Do(req)
}

func (c *Client) Post(url string, data url.Values, makeReq func(r *http.Request)) (*http.Response, error) {
	req, err := http.NewRequest("POST", url, strings.NewReader(data.Encode()))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	if makeReq != nil {
		makeReq(req)
	}
	return c.cli().Do(req)
}
